home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / flash-0.4.3 / player / main.c next >
C/C++ Source or Header  |  1999-01-01  |  6KB  |  264 lines

  1. /////////////////////////////////////////////////////////////
  2. // Flash Plugin and Player
  3. // Copyright (C) 1998 Olivier Debon
  4. // 
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. // 
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. // 
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. // 
  19. ///////////////////////////////////////////////////////////////
  20. //  Author : Olivier Debon  <odebon@club-internet.fr>
  21. //  
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <sys/time.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include "flash.h"
  29. #include <X11/keysym.h>
  30.  
  31. static char *rcsid = "$Id: main.c,v 1.9 1998/12/27 23:07:18 olivier Exp $";
  32.  
  33. /*
  34.  *    This file is the entry of a very simple Flash Player
  35.  */
  36.  
  37. Display *dpy;
  38. GC gc;
  39. Window frame,movie,control;
  40. struct FlashInfo fi;
  41. char *filename;
  42.  
  43. readFile(char *filename, char **buffer, long *size)
  44. {
  45.     FILE *in;
  46.     char *buf;
  47.     long length;
  48.  
  49.     in = fopen(filename,"r");
  50.     if (in == 0) {
  51.         perror(filename);
  52.         exit(2);
  53.     }
  54.     fseek(in,0,SEEK_END);
  55.     length = ftell(in);
  56.     rewind(in);
  57.     buf = malloc(length);
  58.     fread(buf,length,1,in);
  59.     fclose(in);
  60.  
  61.     *size = length;
  62.     *buffer = buf;
  63. }
  64.  
  65. void drawInfo()
  66. {
  67.     char msg[1024];
  68.  
  69.     sprintf(msg,"%s (Flash %d)  - Frames = %d  - Rate = %d fps",
  70.             filename,fi.version,fi.frameCount,fi.frameRate);
  71.  
  72.     XSetForeground(dpy,gc,WhitePixel(dpy, DefaultScreen(dpy)));
  73.     XDrawString(dpy,control,gc,10,15,msg, strlen(msg));
  74.  
  75.     sprintf(msg, "  (Q)uit (R)eplay (P)ause (C)ontinue");
  76.     XDrawString(dpy,control,gc,10,35,msg, strlen(msg));
  77.     XFlush(dpy);
  78. }
  79.  
  80. void playMovie(FlashHandle flashHandle, Display *dpy, Window movie)
  81. {
  82.     struct timeval wd,de,now;
  83.     XEvent event;
  84.     long evMask, cmd;
  85.     fd_set fdset;
  86.     int status;
  87.     long delay = 0;
  88.     long wakeUp = 1;
  89.     long z = 1;
  90.     long x = 0;
  91.     long y = 0;
  92.  
  93.     cmd = FLASH_WAKEUP;
  94.     wakeUp = FlashExec(flashHandle, cmd, 0, &wd);
  95.     XSelectInput(dpy, movie, FLASH_XEVENT_MASK|KeyPressMask);
  96.     XSync(dpy,False);
  97.  
  98.     while(1) {
  99.         FD_ZERO(&fdset);
  100.         FD_SET(ConnectionNumber(dpy),&fdset);
  101.  
  102.         /*printf("WakeUp = %d  Delay = %d\n", wakeUp, delay);*/
  103.         if (delay < 0) {
  104.             delay = 20;
  105.         }
  106.  
  107.         if (wakeUp) {
  108.             de.tv_sec = delay/1000;
  109.             de.tv_usec = (delay%1000)*1000;
  110.             status = select(ConnectionNumber(dpy)+1, &fdset, 0, 0, &de);
  111.         } else {
  112.             status = select(ConnectionNumber(dpy)+1, &fdset, 0, 0, 0);
  113.         }
  114.  
  115.         if (status == 0) {
  116.             cmd = FLASH_WAKEUP;
  117.             wakeUp = FlashExec(flashHandle, cmd, 0, &wd);
  118.         }
  119.         if (status) {
  120.             XNextEvent(dpy, &event);
  121.             /* printf("Event %d (%d)\n",event.type,event.xany.serial); */
  122.  
  123.             if (event.xany.window == movie) {
  124.                 int keycode;
  125.                 KeySym keysym;
  126.  
  127.                 switch (event.type) {
  128.                 case KeyPress:
  129.                     keycode = event.xkey.keycode;
  130.                     keysym = XLookupKeysym((XKeyEvent*)&event, 0);
  131.                     switch (keysym) {
  132.                         case XK_Up:
  133.                             y -= 10;
  134.                             FlashOffset(flashHandle,x,y);
  135.                             break;
  136.                         case XK_Down:
  137.                             y += 10;
  138.                             FlashOffset(flashHandle,x,y);
  139.                             break;
  140.                         case XK_Left:
  141.                             x -= 10;
  142.                             FlashOffset(flashHandle,x,y);
  143.                             break;
  144.                         case XK_Right:
  145.                             x += 10;
  146.                             FlashOffset(flashHandle,x,y);
  147.                             break;
  148.                         case XK_KP_Add:
  149.                             FlashZoom(flashHandle,++z);
  150.                             break;
  151.                         case XK_KP_Subtract:
  152.                             FlashZoom(flashHandle,--z);
  153.                             break;
  154.                         case XK_q:
  155.                             return;
  156.                             break;
  157.                         case XK_c:
  158.                             cmd = FLASH_CONT;
  159.                             wakeUp = FlashExec(flashHandle, cmd, 0, &wd);
  160.                             break;
  161.                         case XK_p:
  162.                             cmd = FLASH_STOP;
  163.                             wakeUp = FlashExec(flashHandle, cmd, 0, &wd);
  164.                             break;
  165.                         case XK_r:
  166.                             cmd = FLASH_REWIND;
  167.                             FlashExec(flashHandle, cmd, 0, &wd);
  168.                             cmd = FLASH_CONT;
  169.                             wakeUp = FlashExec(flashHandle, cmd, 0, &wd);
  170.                             break;
  171.                     }
  172.                     break;
  173.                 case NoExpose:
  174.                     break;
  175.                 default:
  176.                     cmd = FLASH_EVENT;
  177.                     if (FlashExec(flashHandle, cmd, &event, &wd))
  178.                         wakeUp = 1;
  179.                     break;
  180.                 }
  181.             }
  182.             if (event.xany.window == control) {
  183.                 if (event.type == Expose) {
  184.                     drawInfo();
  185.                 }
  186.             }
  187.         }
  188.  
  189.         /* Recompute delay */
  190.         gettimeofday(&now,0);
  191.         delay = (wd.tv_sec-now.tv_sec)*1000 + (wd.tv_usec-now.tv_usec)/1000;
  192.     }
  193. }
  194.  
  195. void
  196. showUrl(char *url, char *target, void *client_data)
  197. {
  198.     printf("GetURL : %s\n", url);
  199. }
  200.  
  201. main(int argc, char **argv)
  202. {
  203.     char *buffer;
  204.     long size;
  205.     FlashHandle flashHandle;
  206.  
  207.     if (argc < 2) {
  208.         fprintf(stderr,"Usage : %s <file.swf>\n", argv[0]);
  209.         exit(1);
  210.     }
  211.  
  212.     dpy = XOpenDisplay(getenv("DISPLAY"));
  213.     if (dpy == 0) {
  214.         fprintf(stderr,"Can't open X display\n");
  215.         exit(1);
  216.     }
  217.     gc = DefaultGC(dpy, DefaultScreen(dpy));
  218.  
  219.     filename = argv[1];
  220.     readFile(filename, &buffer, &size);
  221.  
  222.     flashHandle = FlashParse(buffer, size);
  223.  
  224.     if (flashHandle == 0) {
  225.         exit(1);
  226.     }
  227.  
  228.     FlashGetInfo(flashHandle, &fi);
  229.  
  230.     frame = XCreateSimpleWindow(dpy, RootWindow(dpy, DefaultScreen(dpy)),
  231.                     0, 0,
  232.                     fi.frameWidth/20, fi.frameHeight/20+40,
  233.                     0, WhitePixel(dpy, DefaultScreen(dpy)), BlackPixel(dpy, DefaultScreen(dpy))
  234.                     );
  235.  
  236.     XMapWindow(dpy, frame);
  237.  
  238.     movie = XCreateSimpleWindow(dpy, frame, 0, 0, fi.frameWidth/20,fi.frameHeight/20,
  239.                     0, WhitePixel(dpy, DefaultScreen(dpy)), BlackPixel(dpy, DefaultScreen(dpy))
  240.                     );
  241.  
  242.     XMapWindow(dpy, movie);
  243.  
  244.     control = XCreateSimpleWindow(dpy, frame, 0, fi.frameHeight/20, fi.frameWidth/20,40,
  245.                     0, BlackPixel(dpy, DefaultScreen(dpy)), BlackPixel(dpy, DefaultScreen(dpy))
  246.                     );
  247.  
  248.     XMapWindow(dpy, control);
  249.     XSelectInput(dpy, control, ExposureMask);
  250.     drawInfo();
  251.  
  252.     XFlush(dpy);
  253.  
  254.     FlashGraphicInit(flashHandle, dpy, movie);
  255.  
  256.     FlashSoundInit(flashHandle, "/dev/dsp");
  257.  
  258.     FlashSetGetUrlMethod(flashHandle, showUrl, 0);
  259.  
  260.     playMovie(flashHandle, dpy, movie);
  261.  
  262.     exit(0);
  263. }
  264.